var spider = require('./spider');
var cheerio = require('cheerio');

// setting
var setting = {
    headers: {},
    start: {
        
        timeout: 2000
    },
    isFinish: function (_setting) {
        return false;

    },
    errorHandel: function (err) {
        console.log('-------error catch---------');
        console.log(err);

    },
    successHandel: function (_setting) {
        console.log('-------success ---------');

        spider.loopGetOne(_setting);
    },
    // 解析
    parse: {
        type: 'html',
        charset: 'gb2312',
        handel: function (res, _setting) {

            // console.log(res);

            var $ = cheerio.load(res);  


            var host = "http://comic.kukudm.com";

            var str = $('body').children('table').eq(4).children('tbody').children('tr').children('td').eq(1).find('table').eq(1).children('tbody').children('tr').eq(4).find('td').text().replace(/(^\s*)|(\s*$)/g, "");

            console.log(str);

       
            var arry = str.split('|');

            console.log(arry);

            var _auth = arry[0].split('：')[1];
            var time = arry[2].split('：')[1];
            var isFinish = arry[1].split('：')[1].replace(/(^\s*)|(\s*$)/g, "") == '连载' ? false : true;
            data = {
                auth: _auth.replace(/(^\s*)|(\s*$)/g, ""),
                auth_href: null,
                news: $('#comiclistn dd').last().find('a').eq(0).text().replace(/(^\s*)|(\s*$)/g, ""),
                news_href: host + $('#comiclistn dd').last().find('a').eq(0).attr('href'),
                isFinish: isFinish,
                updateTime: time.replace(/(^\s*)|(\s*$)/g, ""),
                type: null,
                info: $('#ComicInfo').text().replace(/(^\s*)|(\s*$)/g, ""),
                'domain': host
            };

            console.log(data);

            return data;
            // return false;
        }
    },
    mongdb: {
        database: 'manhua',
        collection: 'xinxin',   
    
        type: 'update',
        query: {}
    },
    lastId:'5a4d9b34ebd85b1114c67467'
};

spider.init_d(setting);


